//
//  UIViewController+Networking.m
//  muscleLife
//
//  Created by xalo on 16/4/29.
//  Copyright © 2016年 HeavenAndHell. All rights reserved.
//

#import "UIViewController+Networking.h"

@implementation UIViewController (Networking)

-(void)requsetDataWithUrl:(NSString *)url parameters:(NSDictionary *)parameter finished:(finishedBlock)finish failure:(failureBlock)failure{

    AFHTTPSessionManager* manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url parameters:parameter progress:^(NSProgress * _Nonnull uploadProgress) {

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        finish(dic);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
    }];
}

-(void)getRequestDataWithUrl:(NSString *)url parameters:(NSDictionary *)parameter finished:(finishedBlock)finish failure:(failureBlock)failure{

    AFHTTPSessionManager* manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:url parameters:parameter progress:^(NSProgress * _Nonnull downloadProgress) {

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

        NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        finish(dic);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        failure(error);

    }];
}

//MObApi请求数据
-(void)acquireSourceDataWithKeyWord:(NSString*)keyWord page:(NSString*)page size:(NSString*)sized finished:(mobFinishBlock)block{
    
    [MobAPI sendRequest:[MOBAHealthRequest healthRequestWithKeyword:keyWord page:page size:sized] onResult:^(MOBAResponse *response) {
        block(response);
    }];
}
@end
